<?php

namespace app\api\model;

use app\api\model\Course as CourseModel;
use app\api\model\TeacherCourse as TeacherCourseModel;
use app\common\model\BaseModel;

class TeacherBusy extends BaseModel
{
    /**
     * 教师某天的忙碌时间段
     * @param $teacherId
     * @param $date
     * @param $hour
     * @return array
     */
    public static function getTeacherTimes($teacherId, $date, $hour)
    {
        //教师忙碌时间
        $teacherTimes = self::where('teacher_id', $teacherId)->find();
        if (!$teacherTimes) {
            return [];
        }
        //周几
        $week = getWeekDay($date);
        $weekTimes = explode(',', $teacherTimes[$week]);
        if (!$weekTimes[0]) {
            return [];
        }
        //拆分组合时间段
        $times = [];
        $lastTime = strtotime($date.' 23:59:59');
        foreach ($weekTimes as $val) {
            $t = explode('-', $val);
            $first = intval($t[0]);
            $second = intval($t[1]);
            $secondTime = strtotime($date.' '.$t[1]) + $hour * 3600;
            for ($i = $first; $i <= $second; $i += $hour) {
                $times[] = self::time($date, $hour, $i);
                if (($second - $first) % 2 && $i + $hour > $second && $secondTime <= $lastTime) {
                    $times[] = self::time($date, $hour, $second);
                }
            }
        }
        return $times;
    }

    private static function time($date, $hour, $time)
    {
        $lastTime = strtotime($date.' 23:59:59');
        $startTime = $time < 10 ? '0'.$time : $time;
        $end = strtotime($date.' '.$time.':00') + $hour * 3600;
        if ($end > $lastTime) {
            $endTime = "24:00";
        } else {
            $endTime = date('H:i', $end);
        }
        return $startTime.':00-'.$endTime;
    }

    /**
     * 课程忙碌时间段
     * @param $teacherCouseId
     * @param $date
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function busyTime($teacherCouseId, $date)
    {
        $date = date('Y-m-d', strtotime($date));
        //预约的时间段
        $teacherCourse = TeacherCourseModel::find($teacherCouseId);
        if (!$teacherCourse) {
            static::errorMsg(10001);
        }
        $courseTimes = CourseModel::getTimes($teacherCouseId, $date);
        //教师忙碌时间段
        $teacherTimes = self::getTeacherTimes($teacherCourse->teacher_id, $date, $teacherCourse->hour);
        return array_merge($teacherTimes, $courseTimes);
    }

    /**
     * 批量验证是否忙碌
     * @param $teacherCourseId
     * @param $course
     * @return bool
     */
    public static function isBuys($teacherCourseId, $course)
    {
        foreach ($course as $val) {
            self::isBuy($teacherCourseId, $val['date'], $val['time']);
        }
        return true;
    }

    /**
     * 单个验证是否忙碌
     * @param $teacherCourseId
     * @param $date
     * @param $time
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function isBuy($teacherCourseId, $date, $time)
    {
        $times = self::busyTime($teacherCourseId, $date);
        foreach ($times as $val) {
            $teacherTimes = explode('-', $val);
            $courseTimes = explode('-', $time);
            if (!$teacherTimes[0] || !$courseTimes[0]) {
                continue;
            }
            $teacherTimes0 = strtotime($date. ' '. $teacherTimes[0]);
            $teacherTimes1 = strtotime($date. ' '. $teacherTimes[1]);
            if ($teacherTimes[1] < $teacherTimes[0]) {
                $teacherTimes1 = strtotime('+1 day', strtotime($date. ' '.$teacherTimes[1]));
            }
            $courseTimes0 = strtotime($date. ' '. $courseTimes[0]);
            $courseTimes1 = strtotime($date. ' '. $courseTimes[1]);

            if (($courseTimes0 >= $teacherTimes0 && $courseTimes0 < $teacherTimes1) ||
                ($courseTimes1 > $teacherTimes0 && $courseTimes1 < $teacherTimes1))
            {
                static::error(0, $date.' '.$time.'教师忙碌中');
            }
        }
        return true;
    }
}
